//
//  WCG_M_HistoryDailyController.m
//  Project_B
//
//  Created by 甘霞 on 15/11/25.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "WCG_M_HistoryDailyController.h"
#import "GXiaNetHandle.h"
#import "GXDailyModel.h"
#import "GXHistoryDailyTableViewCell.h"
#import "WCG_M_NoonTeaViewController.h"
#import "WCG_M_HistoryController.h"

@interface WCG_M_HistoryDailyController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *historyArr;

@property (nonatomic, retain) GXDailyModel *dailyModel;
@property (nonatomic, assign) CGFloat startY;
@property (nonatomic, assign) CGFloat endY;

@end

@implementation WCG_M_HistoryDailyController

#pragma mark - 视图将要出现
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //  隐藏导航栏
    [self.navigationController.navigationBar setHidden:NO];
    //  隐藏 tabBar
    NSArray *temp = self.tabBarController.view.subviews;
    UIView *view = temp[2];
    [UIView animateWithDuration:1.0f animations:^{
        view.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 49);
    }];
}

#pragma mark - 视图将要消失
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //  隐藏 tabBar
    NSArray *temp = self.tabBarController.view.subviews;
    UIView *view = temp[2];
    [UIView animateWithDuration:1.0f animations:^{
        view.frame = CGRectMake(0, kScreenHeight - 49, kScreenWidth, 49);
    }];
}

#pragma mark - 视图加载完毕
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //  导航栏
    [self createNaviView];
    //  背景色
    [self.view setBackgroundColor:[UIColor whiteColor]];
    //  创建 tableView
    [self createTableView];
    //  小菊花
    [self loadNightMBProgress];
    //  取得数据
    [self getHistoryData];
    //  创建回退按钮
    [self createButtomBackButtonIsHiddenShareBtn:YES];
    self.buttonView.alpha = 0;
    //  回退按钮添加事件
    [self.backBtn addTarget:self
                     action:@selector(backBtnAction)
           forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 回退按钮响应方法
- (void)backBtnAction {
    //  pop
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 导航栏视图
- (void)createNaviView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon-afternoon-tea-entry"]];
    [imageView setFrame:CGRectMake(0, 0, 40, 40)];
    [view addSubview:imageView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 110, 40)];
    [label setFont:[UIFont fontWithName:@"ArialHebrew-Bold" size:20]];
    label.text = @"往期下午茶";
    [view addSubview:label];
    self.navigationItem.titleView = view;
    self.navigationItem.leftBarButtonItem = nil;
}

#pragma mark - 创建 tableView
- (void)createTableView {
    [self createTableViewWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[GXHistoryDailyTableViewCell class] forCellReuseIdentifier:@"historyCell"];
    self.startY = self.tableView.contentOffset.y;
}

#pragma mark - 请求历史下午茶
- (void)getHistoryData {
    //  初始化数组
    self.historyArr = [NSMutableArray array];
    //  多线程请求数据
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //  请求历史下午茶
        [GXiaNetHandle getUrl:kDailyHistoryWeb
                       cookie:nil
                      success:^(id result) {
                          //    移除小菊花
                          [self removeHud];
                          //    接收数据
                          self.historyArr = [GXDailyModel transformWithArray:result[@"recent_daily"]];
                          //    数据保护
                          (self.historyArr.count != 0) ? [self.tableView reloadData] : nil;
                      } failure:^(NSError *error) {
                          NSLog(@"历史下午茶请求错误:%@", error);
                      }];
    });
}

#pragma mark - tableView Delegate
//  一共有几行
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    
    return (self.historyArr.count != 0) ? self.historyArr.count : 0;
}

//  每行显示什么
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //  cell 重用
    GXHistoryDailyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyCell"];
    //  模型赋值
    cell.dailyModel = self.historyArr[indexPath.row];
    //  返回 cell
    return cell;
}
//  每个 cell 的高度
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //  下午茶控制器
   WCG_M_HistoryController *noonVC = [[WCG_M_HistoryController alloc] init];
    noonVC.release_date = [self.historyArr[indexPath.row] release_date];
    noonVC.dailyModel = self.historyArr[indexPath.row];
    //  push
    [self.navigationController pushViewController:noonVC animated:YES];
}

#pragma mark - 滑动视图正在滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.endY = scrollView.contentOffset.y;
}

#pragma mark - 滑动视图开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    //  向下滚
    if (self.endY - self.startY > 0) {
        //  动画
        [UIView animateWithDuration:3.0f
                         animations:^{
                             // 隐藏底部按钮
                             self.buttonView.alpha = 0;
                         }];
    } else {
        //  动画
        [UIView animateWithDuration:3.0f
                         animations:^{
                             //  显示底部按钮
                             self.buttonView.alpha = 1;
        }];
    }
    //  换值
    self.startY = self.endY;
}

@end
